package vn.appsmobi.widget;

import vn.appsmobi.R;
import vn.appsmobi.loader.BaseResult;
import vn.appsmobi.loader.BaseResult.ResultStatus;
import vn.appsmobi.loader.ProgressNotifiable;
import vn.appsmobi.ulti.Log;
import vn.appsmobi.ulti.ToastUtil;
import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class EmptyLoadingView extends LinearLayout
  implements ProgressNotifiable
{
  private static final String TAG = "EmptyLoadingView";
  private Button mButton;
  private int mEmptyResId;
  private CharSequence mEmptyText;
  private CharSequence mDefautText;
  private ProgressBar mProgressBar;
  private TextView mTextView;
  private Refreshable mRefreshable;
  private NoNewDataCallback mNoNewDataCallback;
  private Context _context;
  
  private View.OnClickListener mOnRefreshListener = new View.OnClickListener()
  {
    public void onClick(View paramAnonymousView)
    {
      if (EmptyLoadingView.this.mRefreshable != null)
        EmptyLoadingView.this.mRefreshable.refreshData();
    }
  };
  public EmptyLoadingView(Context paramContext)
  {
    this(paramContext, null);
  }

  public EmptyLoadingView(Context context, AttributeSet attr)
  {
    super(context, attr);
    _context = context;
    LayoutInflater.from(context).inflate(R.layout.empty_loading, this, true);
    this.mProgressBar = ((ProgressBar)findViewById(R.id.progress));
    this.mTextView = ((TextView)findViewById(R.id.empty_text));
    this.mButton = ((Button)findViewById(R.id.button));
    Log.v("EmptyLoadingView init loading view:" + this.mTextView.getText());
  }

  private void hideView(View view)
  {
    if (view == null)
      return;
    else    	
      if (view.getVisibility() == View.VISIBLE)
      {
        if (view.isShown())
          view.startAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.disappear));
        view.setVisibility(8);
      }
    
  }

  private void showProgressView(boolean hasData, boolean visibile)
  {
   
    if (visibile)
    {
      this.mProgressBar.setVisibility(0);      
      this.mTextView.setVisibility(View.GONE);
    }
    else
    {
      this.mProgressBar.setVisibility(View.VISIBLE);
      this.mTextView.setVisibility(View.VISIBLE);
    }
  }
  
  private void showView(View paramView)
  {
    if (paramView == null)
    	return;
    else      
      if (paramView.getVisibility() == View.GONE)
      {
        paramView.startAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.appear));
        paramView.setVisibility(0);
      }    
  }

  private void updateStyle(boolean hasData)
  {
    if (hasData)
    {
      getLayoutParams().height = -2;
      setBackgroundResource(R.drawable.loading_view_bg);
    }
    else
    {    	
      getLayoutParams().height = -1;
      setBackgroundDrawable(null);
    }
  }

  public void init(boolean hasData, boolean isLoading)
  {
    updateStyle(hasData);
    if (isLoading)
    {
      setVisibility(View.VISIBLE);
      this.mProgressBar.setVisibility(View.VISIBLE);
      this.mTextView.setVisibility(View.GONE);
      this.mButton.setVisibility(View.GONE);
    }
    else
    {      
      if (hasData)
      {
        setVisibility(View.GONE);
      }
      else
      {
        setVisibility(View.VISIBLE);
        this.mProgressBar.setVisibility(View.GONE);
        this.mTextView.setVisibility(View.VISIBLE);
        this.mButton.setVisibility(View.GONE);
      }
    }
  }

  public void onError(boolean hasData, BaseResult.ResultStatus status, final Handler.Callback callback)
  {
    updateStyle(hasData);
    if (hasData)
    {
      hideView(this);
      ToastUtil.show(getContext(), BaseResult.getStatusDes(status));
      return;
    }
    showView(this);
    this.mProgressBar.setVisibility(View.GONE);
    this.mTextView.setVisibility(View.VISIBLE);
    this.mTextView.setText(BaseResult.getStatusDes(status));
    Log.v("EmptyLoadingView set err text:" + this.mTextView.getText());
    this.mButton.setVisibility(0);
    if (status == BaseResult.ResultStatus.NETWROK_ERROR)
      this.mButton.setText(R.string.check_network);
    else
    {
      this.mButton.setOnClickListener(new View.OnClickListener()
      {
        public void onClick(View view)
        {
          callback.handleMessage(null);
        }
      });      
      this.mButton.setText(R.string.data_error);
    }
  }

  public void setEmptyText(int emptyRes)
  {
    this.mEmptyResId = emptyRes;
  }
  
  
  public void setDefaultSuccessText(CharSequence txt)
  {
	  this.mDefautText = txt;
  }
  
  public void setEmptyText(CharSequence errorStr)
  {
    this.mEmptyText = errorStr;
  }
  
  public void setNoNewDataCallback(NoNewDataCallback callback)
  {
    this.mNoNewDataCallback = callback;
  }

  public void setRefreshable(Refreshable refreshable)
  {
    this.mRefreshable = refreshable;
  }

  public void startLoading(boolean hasData)
  {
    updateStyle(hasData);
    this.mProgressBar.setVisibility(View.VISIBLE);
    this.mTextView.setVisibility(View.GONE);
    this.mButton.setVisibility(View.GONE);
    showView(this);
  }


  
  public static abstract interface NoNewDataCallback
  {
    public abstract boolean onNoNewData();
  }

	@Override
	public void stopLoading(boolean hasData, boolean hasNext) {
	if (!hasNext)
    {
        updateStyle(hasData);
        hideView(this);
        if (!hasData)
        	showView(this);
        hideView(this);
      }
	else
      {
        showProgressView(hasData, false);
        this.mTextView.setVisibility(View.VISIBLE);
      }	
	}

	@Override
	public void stopLoading(boolean hasData, boolean hasNext, ResultStatus errorCode) {
		Log.v("hasData:" + hasData + "hasNext:" + hasNext + "errorCode" + errorCode);
		stopLoading(hasData, hasNext);
		if(!hasNext)
		{
			if(errorCode == BaseResult.ResultStatus.NETWROK_ERROR)
			{
				
			}
			else if(errorCode == BaseResult.ResultStatus.OK)
			{
				if(!hasData)
				{
					if ((this.mNoNewDataCallback == null) || (!this.mNoNewDataCallback.onNoNewData()))
		               Toast.makeText(_context,_context.getString(R.string.data_not_exist),Toast.LENGTH_SHORT).show();
				}
			}
		}
		else
		{
			return;
		}		
	}
}
